import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

export default class Viewer {
  constructor(container) {
    this.container = container;
    this.raycasterObjects = [];

    this.initScene();
    this.initCamera();
    this.initLights();
    this.initRenderer();
    this.initControls();
    this.initRaycaster();
  }

  initScene() {
    this.scene = new THREE.Scene();
    // this.scene.add(new THREE.AxesHelper(10));
    // const gridHelper = new THREE.GridHelper(30, 30);
    // // 设置网格材质为透明
    // gridHelper.material.transparent = true;
    // gridHelper.material.opacity = 0.3;
    // gridHelper.position.y = -0.1;
    // this.scene.add(gridHelper);

    // this.scene.background = new THREE.Color("#333333");
  }

  //初始化场景
  initCamera() {
    this.camera = new THREE.PerspectiveCamera(75, this.container.value.clientWidth / this.container.value.clientHeight, 0.1, 1000);
    this.camera.position.set(0, 10, 15); // 摄像机位置
    this.camera.lookAt(0, 0, 0); //设置相机方向
    this.scene.add(this.camera);
  }

  //初始化灯光
  initLights() {
    // 添加环境光（辅助照明）
    const ambient = new THREE.AmbientLight(0xffffff, 1); // 创建环境光
    this.scene.add(ambient);

    // 添加半球光（模拟天空和地面反射）
    const hemisphereLight = new THREE.HemisphereLight("#ffffff", 0x080820, 1);
    this.scene.add(hemisphereLight);
    // 添加主光源（方向光）
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.position.set(0, 50, 10);
    this.scene.add(directionalLight);
  }

  //初始化渲染器
  initRenderer() {
    this.renderer = new THREE.WebGLRenderer({
      antialias: true //是否开启抗锯齿
      // alpha: true, //是否可以设置背景色透明
      // precision: "mediump" ///设置渲染精度(highp | mediump | lowp)
    });
    // this.renderer.clearDepth(); //清除深度缓存
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(this.container.value.clientWidth, this.container.value.clientHeight);

    this.renderer.shadowMap.enabled = true; //显示阴影
    // this.renderer.setClearColor("#171A29", 1); // 设置背景颜色
    this.container.value.appendChild(this.renderer.domElement);
  }

  //渲染场景
  render() {
    this.renderer.render(this.scene, this.camera);
    this.controls.update();
  }

  //场景更新
  update() {}

  // 调整窗口大小
  resize(width, height) {
    this.camera.aspect = width / height; // 重置摄像机视锥体的长宽比
    this.camera.updateProjectionMatrix(); //更新摄像机投影矩阵
    this.renderer.setSize(width, height); // 重置画布大小
  }

  //初始化控制器
  initControls() {
    this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    this.controls.minDistance = 2;
    this.controls.maxDistance = 1000;
    //限制角度
    this.controls.maxPolarAngle = Math.PI / 2;
    this.controls.minPolarAngle = -Math.PI / 2;
  }

  //初始化光线投射，用于鼠标拾取
  initRaycaster() {
    this.raycaster = new THREE.Raycaster();
  }

  //设置鼠标拾取的模型数组
  setRaycasterObjects(objList) {
    this.raycasterObjects = objList;
  }

  getRaycasterIntersectObjects({ clientX, clientY }) {
    if (!this.raycasterObjects.length) return null;

    const { width, height } = this.renderer.domElement;
    const pointer = new THREE.Vector2();
    //鼠标的canvas坐标转裁剪坐标
    pointer.set((clientX / width) * 2 - 1, -(clientY / height) * 2 + 1);
    this.raycaster.setFromCamera(pointer, this.camera);
    //返回拾取模型数组
    let intersects = this.raycaster.intersectObjects(this.raycasterObjects);
    return intersects.length ? intersects[0].object : null;
  }

  dispose() {
    if (this.renderer) {
      this.renderer.dispose();
      if (this.container.value && this.renderer.domElement.parentNode === this.container.value) {
        this.container.value.removeChild(this.renderer.domElement);
      }
    }
  }
}
